﻿using System;
using System.IO;
using System.Web;
using Framework;

namespace Enterprise.Admin.Handle
{
    /// <summary>
    /// 上传工作设备照片
    /// </summary>
    public partial class UploadWorkShopPicture : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            object result;
            try
            {
                var file = Request.Files["Filedata"];
                if (file == null)
                {
                    result = (new { state = false, message = "上传文件不能为空！" });
                }
                else
                {
                    var filedata = new HttpPostedFileWrapper(Request.Files["Filedata"]);
                    var uploadHelper = new UploadHelper
                    {
                        PostedFile = filedata,
                        RelativePath = WorKShopConfig.RelativePath,
                        IsAdaptive = WorKShopConfig.IsAdaptive,
                        IsImage = true,
                        IsGenerateThumbnail = WorKShopConfig.IsGenerateThumbnail,
                        FileMaxLength = WorKShopConfig.ImageSize,
                        ThumbnailHeight = WorKShopConfig.ThumbnailHeight,
                        ThumbnailWidth = WorKShopConfig.ThumbnailWidth,
                        ThumbnailPrefix = WorKShopConfig.ThumbnailPrefix
                    };
                    if (!uploadHelper.Save())
                    {
                        throw new Exception(uploadHelper.ErrorMessage);
                    }
                    string fileName = uploadHelper.FileName ?? string.Empty;
                    string thumFileName = uploadHelper.ThumbnailFileName ?? string.Empty;
                    result = (new { state = true, message = "上传成功！", fileName, thumFileName });
                }

            }
            catch (Exception ex)
            {
                result = (new { state = false, message = ex.Message });
            }
            Response.Write(result.Serialization());
            Response.End();
        }
    }
}